package com.lai.berry;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.lai.berry_core.delegate.BerryDelegate;
import com.lai.berry_core.net.RestClient;
import com.lai.berry_core.net.callback.ISuccess;
import com.lai.berry_core.net.rx.RxRestClient;
import com.lai.berry_core.ui.loader.BerryLoader;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


public class ExampleDelegate extends BerryDelegate{

    @Override
    public Object setLayout() {
        return R.layout.delegate_example;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        //testRestClient();
        onCallRxGet();
    }

    private void testRestClient() {

        RestClient.builder()
                .url("http://127.0.0.1/index")
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String respose) {
                        Toast.makeText(getActivity(), ""+respose, Toast.LENGTH_SHORT).show();
                    }
                })
                .build().get();

    }

    // TODO: 2017/10/28 搞完提裤子
    private void onCallRxGet(){

        RxRestClient.builder()
                .url("https://gitee.com/alfried_lfl/FileManager/raw/master/user.json")
                .loader(getContext())
                .build()
                .get()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(@NonNull String s) throws Exception {
                        BerryLoader.stopLoading();
                        Toast.makeText(_mActivity, "1" + s, Toast.LENGTH_SHORT).show();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        Log.e("sdfsfsf111", "accept: " + throwable.getMessage());
                    }
                });

    }

}
